<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\System\File;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FileService
{

    use Service;

    /**
     * 我的文件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FileIndexService( $request )
    {
        $user   = $this->getCommon()->UserCommonService();
        $where  = function ( $query ) use ( $request, $user ) {
            $query->where('status', 1);
            $query->where('uid', $user->id);
            if ( $request->filled('name') ) $query->where('name', 'like', "%" . $request->input('name') . "%");
            if ( $request->filled('created_at') ) {
                $time       = $request->input('created_at');
                $start_time = $time[ 0 ];
                $end_time   = $time[ 1 ];
                $query->whereBetween('created_at', [ $start_time, $end_time ]);
            }
        };
        $select = [ 'id', 'size', 'name', 'url', 'created_at' ];
        $File   = File::PaginateModel($where, [], $select, $request->input('limit'));
        return data($File);
    }

    /**
     * 删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FileDestroyService( $id )
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ( $query ) use ( $id, $user ) {
            $query->where('uid', $user->id);
            if ( ! is_array($id) ) $query->where('id', $id);
            else  $query->whereIn('id', $id);
        };
        File::UpdateModel($where, [
            'status'     => 2,
            'deleted_at' => Carbon::now()->toDateTimeString(),
        ]);
        return data(NULL, $this->getErrorCode()::message(204));
    }

}
